package com.hollycrm.ahunicom.sheet.business.logic.hibernate;

import org.springframework.orm.hibernate.HibernateTemplate;
import org.springframework.orm.hibernate.support.HibernateDaoSupport;

import com.hollycrm.unicom.security.AuthToken;
import com.hollycrm.unicom.sheet.Constants;
import com.hollycrm.unicom.sheet.domain.DealSheet;
import com.hollycrm.unicom.sheet.domain.MainSheet;
import com.hollycrm.unicom.sheet.flow.SheetAction;
import com.hollycrm.unicom.sheet.flowsupport.ActionExecuteLogic;
import com.hollycrm.unicom.sheet.flowsupport.DoOperateContext;

public class HibernateEndDealSheetLogic extends HibernateDaoSupport implements
		ActionExecuteLogic {
    /**
     * execute
     *
     * @param authToken AuthToken
     * @param action SheetAction
     * @param context DoOperateContext
     */
    public void execute(AuthToken authToken, SheetAction action,
                        DoOperateContext context) {
        HibernateTemplate ht = getHibernateTemplate();
        String currentTime = (String) context.getOutputAttr("startTime");
        //----------------------------------------------------------------------
        String dealSheetFlowNO = (String) context.getInputAttr("sheetFlowNO");
        String reasonContent = (String) context.getInputAttr("reasonContent");
        String dealContent = (String) context.getInputAttr("dealContent");
        String result = (String) context.getInputAttr("result");
        String spareContentField1 = (String) context.getInputAttr("spareContentField1");
           //----------------------------------------------------------------------
        DealSheet dealSheet = (DealSheet) ht.load(DealSheet.class,
                                                  dealSheetFlowNO);
        dealSheet.setDealContent(dealContent);
        dealSheet.setReasonContent(reasonContent);
        dealSheet.setSpareContentField1(spareContentField1);

        logger.error("xxxxx " + dealSheet.getSpareContentField1());

        if (result == null)
            result = action.getAttributeValue("result");
        dealSheet.setResult(result);
        String status = action.getAttributeValue("status");
        dealSheet.setStatus(status);
        dealSheet.setDealTime(currentTime);
        dealSheet.setDealAgentCode(authToken.getUserCode());
        dealSheet.calculateCostTime();
        //----------------------------------------------------------------------
        updateSheetStatus(dealSheet, action,context);
        ht.update(dealSheet);
        //----------------------------------------------------------------------
        setupOutputAttr(context,dealSheet.getSheetFlowNO(),dealSheet);
        context.addOutputAttr(dealSheet.getMainSheet());
    }

    /**
     * updateSheetStatus
     *
     * @param dealSheet DealSheet
     * @param action SheetAction
     */
    private void updateSheetStatus(DealSheet dealSheet, SheetAction action,DoOperateContext context) {
        HibernateTemplate ht = getHibernateTemplate();
        //----------------------------------------------------------------------
        String resultFlag = (String) context.getInputAttr("resultFlag");
        if(resultFlag==null) resultFlag="";
        if (dealSheet.getParentSheetFlowNO() == null) {
            MainSheet mainSheet = (MainSheet) ht.load(MainSheet.class,
                                                      dealSheet.getMainSheetFlowNO());
            String m_status = action.getAttributeValue("m_status");
            if (m_status == null)
                m_status = Constants.SHEET_STATUS_COMPLETE;
            if(!mainSheet.isEnd()){
                mainSheet.setReturnGroupMark(dealSheet.getGroupMark());
                mainSheet.setStatus(m_status);
                mainSheet.setResultFlag(resultFlag);
                ht.update(mainSheet);
            }
        }
        else {
            DealSheet parentSheet = (DealSheet) ht.load(DealSheet.class,
                                                        dealSheet.getParentSheetFlowNO());
            MainSheet mainSheet = (MainSheet) ht.load(MainSheet.class,
                    dealSheet.getMainSheetFlowNO());
            {
            	  if(!mainSheet.isEnd()){
                      mainSheet.setResultFlag(resultFlag);
                      ht.update(mainSheet);
            	  }
            }
            String p_status = action.getAttributeValue("p_status");
            if (p_status == null)
                p_status = Constants.SHEET_STATUS_COMPLETE;
            if(!parentSheet.isEnd()){
                parentSheet.setStatus(p_status);
                ht.update(parentSheet);
            }
        }
    }

    private void setupOutputAttr(DoOperateContext context,
                                 String dealSheetFlowNO, DealSheet dealSheet) {
        context.addOutputAttr("mainSheetFlowNO", dealSheet.getMainSheetFlowNO());
        context.addOutputAttr("parentSheetFlowNO",
                             dealSheet.getParentSheetFlowNO()!=null?dealSheet.getParentSheetFlowNO():"");
        context.addOutputAttr("operationObject",dealSheet.getSheetFlowNO());
        context.addOutputAttr(dealSheet);
    }
}
